package project.servlets;

import java.io.IOException;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import schronisko.MySQLConnection;
import schronisko.Queries;

/**
 * Servlet implementation class Request
 */
@WebServlet("/Request")
public class Request extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	Queries q = new Queries();
	HttpSession s;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Request() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		s = request.getSession();
		String query = request.getParameter("sql");
		String type = request.getParameter("type");
		if(type != null){
		if(type.equals("select")){
			ResultSet r = q.select(query);
			s.setAttribute("select_result", r); // wyniki select
			response.sendRedirect("admin/request_r.jsp");
		}
		else if(type.equals("insert")){
			int rows = q.insert(query);
			if(rows > 0)
			s.setAttribute("msg_req", 2); // poprawnie wykonano
			else
				s.setAttribute("msg_req", 3); // nie wykonano
			response.sendRedirect("admin/request.jsp");
		}
	}
		else {
			s.setAttribute("msg_req", 1); // brak zaznaczenia typu
			response.sendRedirect("admin/request.jsp");
		}
	}
	

}
